package com.rt.easyjava.base.biz1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rt.easyjava.base.biz1.bean.TmptAppVersion;
import com.rt.easyjava.base.biz1.dto.TmptAppVersionDto;
import com.rt.easyjava.base.biz1.mapper.TmptAppVersionMapper;
import com.rt.easyjava.base.biz1.service.intf.TmptAppVersionService;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.config.dozer.MyMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 *  服务实现类
 * @author wuxiaoyong
 * @since 2020-05-10
 */
@Service(value = "tmptAppVersionService")
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class TmptAppVersionServiceImpl extends ServiceImpl<TmptAppVersionMapper, TmptAppVersion> implements TmptAppVersionService {
    private final Logger logger = LoggerFactory.getLogger(TmptAppVersionServiceImpl.class);

    @Autowired
    private MyMapper mapper;
    @Resource
    private TmptAppVersionMapper tmptAppVersionMapper;

    // ==================================================================================
    // ========== 以下使用的是 Mybatis-plus 实现, 适合单表操作 ==============================
    // ==================================================================================

    /**
    * create a object
    * @param tmptAppVersionDto
    * @return
    */
    @Override
    public Result<TmptAppVersionDto> create(TmptAppVersionDto tmptAppVersionDto) {
        Result<TmptAppVersionDto> result = new Result<>();

        // dto to entity
        TmptAppVersion tmptAppVersion = mapper.map(tmptAppVersionDto, TmptAppVersion.class);

        tmptAppVersionMapper.insert(tmptAppVersion);

        return result.success();
    }

    /**
    * update a object by Id
    * @param tmptAppVersionDto
    * @param updateWrapper
    * @return
    */
    @Override
    public Result<TmptAppVersionDto> update(TmptAppVersionDto tmptAppVersionDto, UpdateWrapper updateWrapper) {
        Result<TmptAppVersionDto> result = new Result<>();

        // dto to entity
        TmptAppVersion tmptAppVersion = mapper.map(tmptAppVersionDto, TmptAppVersion.class);

        tmptAppVersionMapper.update(tmptAppVersion, updateWrapper);

        return result.success();
    }

    /**
    * 分页 (无合计功能)
    * @param page
    * @param queryWrapper
    * @return
    */
    public Result<TmptAppVersionDto> page(Page<TmptAppVersion> page, QueryWrapper<TmptAppVersion> queryWrapper){
        Result<TmptAppVersionDto> result = new Result<>();

        // mapper
        IPage<TmptAppVersion> iPage = tmptAppVersionMapper.selectPage(page, queryWrapper);

        // 查询结果转换成DTO
        List<TmptAppVersionDto> tmptAppVersionDtoList= mapper.maplist(iPage.getRecords(), TmptAppVersionDto.class);

        result.getMap().put("total", iPage.getTotal());
        result.getMap().put("rows", tmptAppVersionDtoList);

        return result.success();
    }

    /**
    * select all by queryWrapper
    * @param queryWrapper
    * @return
    */
    @Override
    public Result<TmptAppVersionDto> selectBy(QueryWrapper<TmptAppVersion> queryWrapper) {
        Result<TmptAppVersionDto> result = new Result<>();

        List<TmptAppVersion> list = tmptAppVersionMapper.selectList(queryWrapper);

        List<TmptAppVersionDto> dtoList = mapper.maplist(list, TmptAppVersionDto.class);

        return result.setList(dtoList).success();
    }

    /**
    * select one by id
    * @param id
    * @return
    */
    @Override
    public Result<TmptAppVersionDto> selectById(Long id) {
        Result<TmptAppVersionDto> result = new Result<>();

        TmptAppVersion tmptAppVersion = tmptAppVersionMapper.selectById(id);

        TmptAppVersionDto tmptAppVersiondto = mapper.map(tmptAppVersion, TmptAppVersionDto.class);

        return result.setObj(tmptAppVersiondto).success();
    }




    // ==================================================================================
    // ========== 以下使用的是自定义的 mapper.xml 实现, 适合多表查询 =========================
    // ==================================================================================

    /**
    * 分页查询：mybatis plus 自定义sql分页查询
    * 默认开启合计查询，按需注掉代码
    * @param page
    * @param queryWrapper
    * @return
    */
    @Override
    public Result<TmptAppVersionDto> myPage(Page<TmptAppVersion> page, QueryWrapper<TmptAppVersion> queryWrapper) {
        Result<TmptAppVersionDto> result = new Result();

        IPage<TmptAppVersion> iPage = tmptAppVersionMapper.myPage(page, queryWrapper);
        // 查询结果转换成DTO
        List<TmptAppVersionDto> tmptAppVersionDtoList = mapper.maplist(iPage.getRecords(), TmptAppVersionDto.class);
        // 返回结果集
        result.getMap().put("total", iPage.getTotal());
        result.getMap().put("rows", tmptAppVersionDtoList);

        // 合计
        List<TmptAppVersion> sumList = tmptAppVersionMapper.mySelectSum(queryWrapper);
        List<TmptAppVersionDto> sumDtoList = mapper.maplist(sumList, TmptAppVersionDto.class);
        result.getMap().put("footer", sumDtoList);

        return result.success();
    }

    @Override
    public Result<TmptAppVersionDto> mySelectBy(QueryWrapper<TmptAppVersion> queryWrapper) {
        Result<TmptAppVersionDto> result = new Result();

        List<TmptAppVersion> list = tmptAppVersionMapper.mySelectBy(queryWrapper);
        List<TmptAppVersionDto> dtoList = mapper.maplist(list, TmptAppVersionDto.class);
        result.setList(dtoList);

        return result.success();
    }

    @Override
    public Result<TmptAppVersionDto> mySelectById(long id) {
        Result<TmptAppVersionDto> result = new Result();

        TmptAppVersion tmptAppVersion = tmptAppVersionMapper.mySelectById(id);
        TmptAppVersionDto dto = mapper.map(tmptAppVersion,TmptAppVersionDto.class);
        result.setObj(dto);

        return result.success();
    }



}
